#!/bin/bash


args_ip=$1
args_pwd=$2
args_name=$3

#bt_token=$4
#bt_token_crypt=$5

mods=$4

version=$5

# echo "args_ip=$args_ip"
# echo "args_pwd=$args_pwd"
# echo "args_name=$args_name"
cd ~
timedatectl set-timezone Asia/Shanghai

rm ~/install.lock
touch ~/install.lock
echo "ip: $args_ip" >> ~/install.lock
echo "pwd: $args_pwd" >> ~/install.lock
#echo "token: $bt_token_crypt" >> ~/install.lock
echo "mods: $mods" >> ~/install.lock
echo `date +"%Y-%m-%d %H:%M:%S" ` '00% start' >> ~/install.lock


if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
    PM="yum"
elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
    PM="apt-get"
fi

if [ ! -f "/usr/bin/unzip" ]; then
    if [ "${PM}" = "yum" ]; then
        yum install unzip -y
    elif [ "${PM}" = "apt-get" ]; then
        apt-get update
        apt-get install unzip -y
    fi
fi



# 开放3306端口
sudo ufw allow 3306
sudo ufw allow 8085
sudo ufw allow 3724
sudo ufw allow 7878
sudo ufw reload


sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
sudo rm /var/cache/apt/archives/lock
# apt-get install tmux
# tmux kill-session -t install
# tmux new -s install
# sh ./server_install.sh 115.231.221.249 53957105 哈韩





#echo `date +"%Y-%m-%d %H:%M:%S" ` '10% installBT' >> ~/install.lock

#安装宝塔
# wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && echo y | sudo bash install.sh ed8484bec

#设置宝塔接口
#touch /www/server/panel/config/api.json
#echo '{"open": true, "token": "'$bt_token'", "limit_addr": ["148.70.31.191"], "token_crypt": "'$bt_token_crypt'"}' > /www/server/panel/config/api.json
#bt restart


echo `date +"%Y-%m-%d %H:%M:%S" ` '10% downloadSrc' >> ~/install.lock

#下载源码

cd /
if [ ! -d "/azerothcore/" ]; then
    if [ "$version" = "npcbot" ];then
        git clone https://gitee.com/53957105/AzerothCore-wotlk-with-NPCBots --branch cn --single-branch azerothcore
    else
        git clone https://gitee.com/53957105/azerothcore-wotlk-github.git --branch cn --single-branch azerothcore
        cd /azerothcore/modules
        git clone https://gitee.com/53957105/mod-eluna-github.git --single-branch mod-eluna
    fi
fi
#if [ $version -gt 0 ];then
#    git checkout --no-track -b Branch_$version $version
#fi

echo `date +"%Y-%m-%d %H:%M:%S" ` '20% installMod' >> ~/install.lock

###[modsInstall]###


echo `date +"%Y-%m-%d %H:%M:%S" ` '40% startBuild' >> ~/install.lock
touch ~/install.lock

#编译源码
cd /azerothcore
cp conf/dist/config.sh conf/
./acore.sh install-deps && ./acore.sh compiler all

echo `date +"%Y-%m-%d %H:%M:%S" ` '50% setDatabase' >> ~/install.lock

#设置mysql
mysql -uroot <<EOF
DROP USER IF EXISTS 'root'@'%';
CREATE USER 'root'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'root'@'%';
ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
ALTER user 'root'@'%' IDENTIFIED by '$args_pwd';
ALTER user 'root'@'localhost' IDENTIFIED by '$args_pwd';
DROP USER IF EXISTS 'acore'@'localhost';
DROP USER IF EXISTS 'acore'@'127.0.0.1';
CREATE USER 'acore'@'localhost' IDENTIFIED BY 'acore';
CREATE USER 'acore'@'127.0.0.1' IDENTIFIED BY 'acore';
GRANT ALL PRIVILEGES ON * . * TO 'acore'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON * . * TO 'acore'@'127.0.0.1' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF

echo `date +"%Y-%m-%d %H:%M:%S" ` '60% initDatabase' >> ~/install.lock

# 初始化数据库
if [ -f "/azerothcore/apps/db_assembler/conf.dist.sh"  ]; then
    cp /azerothcore/apps/db_assembler/conf.dist.sh /azerothcore/apps/db_assembler/config.sh
fi

sh ./apps/db_assembler/db_assembler.sh import-bases
sh ./apps/db_assembler/db_assembler.sh import-customs
#./acore.sh db-assembler import-all
#./acore.sh db-assembler all

echo `date +"%Y-%m-%d %H:%M:%S" ` '70% setConfig' >> ~/install.lock

# 设置reamlist
mysql -uroot <<EOF
use acore_auth;
update realmlist set address='$args_ip',name='$args_name';
INSERT INTO account_access(id,gmlevel) values(1,3);
EOF

#复制配置文件
cp env/dist/etc/authserver.conf.dist env/dist/etc/authserver.conf
cp env/dist/etc/worldserver.conf.dist env/dist/etc/worldserver.conf

echo '[worldserver]'> env/dist/etc/worldserver.conf
echo 'SOAP.Enabled = 1'> env/dist/etc/worldserver.conf
echo 'SOAP.IP = "0.0.0.0"'>> env/dist/etc/worldserver.conf

#设置允许远程登录
echo '[mysqld]' >>  /etc/mysql/my.cnf
echo 'bind-address            = 0.0.0.0' >>  /etc/mysql/my.cnf
service mysql restart


echo `date +"%Y-%m-%d %H:%M:%S" ` '80% downloadMap' >> ~/install.lock

# 下载data
cd ~
if [ ! -d "/root/data.zip" ]; then
    #wget https://cdn.githubjs.cf/wowgaming/client-data/releases/download/v15/data.zip
    #wget https://hub.fastgit.xyz//wowgaming/client-data/releases/download/v15/data.zip
    wget http://hx.phasercn.com/client-data/releases/download/v15/data.zip
fi

echo `date +"%Y-%m-%d %H:%M:%S" ` '90% unzipMapFile' >> ~/install.lock

cd /azerothcore/env/dist/bin
unzip -q ~/data.zip


echo `date +"%Y-%m-%d %H:%M:%S" ` '100% success' >> ~/install.lock
touch ~/install.lock
# cd /azerothcore
# tmux kill-session -t authserver
# tmux new -s authserver
# tmux send -t authserver "cd /azerothcore && ./acore.sh run-authserver" Enter
# tmux kill-session -t worldserver
# tmux new -s worldserver
# tmux send -t worldserver "cd /azerothcore && ./acore.sh run-worldserver" Enter
# tmux attach -t install
# tmux send -t install "cd /root && sh ./install_bash.sh 115.231.221.249 53957105 哈哈" Enter
# git clean -dfx
# mysql -u root -p -e "DROP USER IF EXISTS 'acore'@'localhost';"
